/**
 * OPay Inc.
 * Copyright (c) 2016-2022 All Rights Reserved.
 */
package cc.dd.marketing.system.exception.handle;

import cc.dd.marketing.api.constant.ResultDTO;
import cc.dd.marketing.system.exception.base.BaseException;
import cc.dd.marketing.system.exception.response.ResultCodeEnum;
import cc.dd.marketing.common.ResultUtil;
import cc.dd.marketing.system.exception.base.BusinessException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.net.BindException;

/**
 * @author sufeng.dong
 * @version $Id: UnifiedExceptionHandler.java, v 0.1 2022-05-24 2:14 PM sufeng.dong Exp $$
 */

@Component
@ControllerAdvice
@Log4j2
public class UnifiedExceptionHandler {


    /**
     * 业务异常
     *
     * @param e 异常
     * @return 异常结果
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public ResultDTO handleBusinessException(BusinessException e) {
        log.error(e.getMessage(), e);
        return ResultUtil.fail(e.getCode(), e.getMessage());
    }

    /**
     * 自定义异常
     *
     * @param e 异常
     * @return 异常结果
     */
    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public ResultDTO handleBaseException(BaseException e) {
        log.error(e.getMessage(), e);
        return ResultUtil.fail(e.getCode(), e.getMessage());
    }

    /**
     * Controller上一层相关异常
     *
     * @param e 异常
     * @return 异常结果
     */
    @ExceptionHandler(value = {
            NoHandlerFoundException.class,
            HttpRequestMethodNotSupportedException.class,
            HttpMediaTypeNotSupportedException.class,
            MissingPathVariableException.class,
            MissingServletRequestParameterException.class,
            TypeMismatchException.class,
            HttpMessageNotReadableException.class,
            HttpMessageNotWritableException.class,
            // BindException.class,
            // MethodArgumentNotValidException.class
            HttpMediaTypeNotAcceptableException.class,
            ServletRequestBindingException.class,
            ConversionNotSupportedException.class,
            MissingServletRequestPartException.class,
            AsyncRequestTimeoutException.class
    })
    @ResponseBody
    public ResultDTO handleServletException(Exception e) {
        log.error(e.getMessage(), e);
        return ResultUtil.fail(ResultCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }


    /**
     * 参数绑定异常
     *
     * @param e 异常
     * @return 异常结果
     */
    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public ResultDTO handleBindException(BindException e) {
        log.error("参数绑定校验异常", e);
        return ResultUtil.fail(ResultCodeEnum.PARAM_ERROR.getCode(), e.getMessage());
    }

    /**
     * 参数校验异常，将校验失败的所有异常组合成一条错误信息
     *
     * @param e 异常
     * @return 异常结果
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public ResultDTO handleValidException(MethodArgumentNotValidException e) {
        log.error("参数绑定校验异常", e);
        return ResultUtil.fail(ResultCodeEnum.PARAM_ERROR.getCode(), e.getMessage());
    }


    /**
     * 未定义异常
     *
     * @param e 异常
     * @return 异常结果
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultDTO handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ResultUtil.fail(ResultCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }
}
